<template>
  <div class="largeScreen_main">
    <div class="largeScreen_header">
      <div class="largeScreen_header_title">设备功率</div>
    </div>
    <div
      v-if="deviceIds.length>0&&tableData&&(tableData.data1.deviceName||
        tableData.data2.deviceName||tableData.data3.deviceName||tableData.data4.deviceName)"
      class="echarts_container"
    >
      <div v-if="tableData&&tableData.data1.deviceName" class="echarts_item">
        <div ref="EquipmentPower_echartsId1" class="echarts_item_top" />
        <div class="echarts_item_content1">{{ tableData&&tableData.data1?tableData.data1.equipmentPower:0 }}</div>
        <div class="echarts_item_content2">{{ tableData&&tableData.data1?tableData.data1.unit:'w' }}</div>
        <div class="echarts_item_content3">{{ tableData&&tableData.data1?tableData.data1.deviceName:'' }}</div>
      </div>
      <div v-if="tableData&&tableData.data2.deviceName" class="echarts_item">
        <div ref="EquipmentPower_echartsId2" class="echarts_item_top" />
        <div class="echarts_item_content1">{{ tableData&&tableData.data2?tableData.data2.equipmentPower:0 }}</div>
        <div class="echarts_item_content2">{{ tableData&&tableData.data2?tableData.data2.unit:'w' }}</div>
        <div class="echarts_item_content3">{{ tableData&&tableData.data2?tableData.data2.deviceName:'' }}</div>
      </div>
      <div v-if="tableData&&tableData.data3.deviceName" class="echarts_item">
        <div ref="EquipmentPower_echartsId3" class="echarts_item_top" />
        <div class="echarts_item_content1">{{ tableData&&tableData.data3?tableData.data3.equipmentPower:0 }}</div>
        <div class="echarts_item_content2">{{ tableData&&tableData.data3?tableData.data3.unit:'w' }}</div>
        <div class="echarts_item_content3">{{ tableData&&tableData.data3?tableData.data3.deviceName:'' }}</div>
      </div>
      <div v-if="tableData&&tableData.data4.deviceName" class="echarts_item">
        <div ref="EquipmentPower_echartsId4" class="echarts_item_top" />
        <div class="echarts_item_content1">{{ tableData&&tableData.data4?tableData.data4.equipmentPower:0 }}</div>
        <div class="echarts_item_content2">{{ tableData&&tableData.data4?tableData.data4.unit:'w' }}</div>
        <div class="echarts_item_content3">{{ tableData&&tableData.data4?tableData.data4.deviceName:'' }}</div>
      </div>
    </div>
    <div v-else class="echarts_noData">
      <svg-icon icon-class="暂无数据" />
    </div>

  </div>
</template>

<script>
import { equipmentPower } from '@/api/system/largeScreen'
import { mapGetters } from 'vuex'
export default {
  props: {
    deviceIds: {}
  },
  computed: {
    ...mapGetters([
      'windowResizeTag'
    ])
  },
  watch: {
    windowResizeTag() {
      if (this.myChart || this.vertical) {
        this.$nextTick(() => {
          setTimeout(() => {
            this.drawLine()
          }, 500)
        })
      }
    }
  },
  data() {
    return {
      tableData: {
        data1: {},
        data2: {},
        data3: {},
        data4: {}
      },
      echarts1: undefined,
      echarts2: undefined,
      echarts3: undefined,
      echarts4: undefined,

      timer: undefined
    }
  },
  mounted() {
    console.log(this.deviceIds)
    this.getData()
    this.timer = setInterval(() => {
      this.getData()
    }, this.$store.state.common.refreshFrequency * 1000)

    // todo 缩放待研究
    // this.$nextTick(() => {
    //   console.log(document.getElementsByClassName('echarts_item')[0].offsetHeight)
    //   console.log(document.getElementsByClassName('echarts_item')[0].offsetWidth)
    // })
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    getData() {
      equipmentPower({ companyId: this.$store.state.user.companyId }).then(res => {
        const resData = this.$deepClone(res.data)
        console.log(resData)
        resData.forEach(item => {
          if (item.maxPower <= 1) {
            item.unit = 'w'
            item.equipmentPower = item.equipmentPower * 1000
            item.maxPower = item.maxPower * 1000
          } else {
            item.unit = 'kw'
          }
        })
        if (this.deviceIds && this.deviceIds.length > 0) {
          this.deviceIds.forEach((item, index) => {
            if (resData.filter(item2 => item2.companyDeviceId == item).length != 0) {
              this.tableData['data' + (index + 1)] = resData.filter(item2 => item2.companyDeviceId == item)[0]
            } else {
              this.tableData['data' + (index + 1)] = {}
            }
            if (index == this.deviceIds.length - 1) {
              console.log(this.tableData)
              this.$nextTick(() => {
                this.drawLine()
              })
            }
          })
        }
      })
    },
    drawLine() {
      if (this.tableData.data1.companyDeviceId) {
        if (this.echarts1) {
          this.$echarts.init(this.$refs['EquipmentPower_echartsId1']).dispose()
        }
        this.echarts1 = this.$echarts.init(this.$refs['EquipmentPower_echartsId1'])
        this.echarts1.setOption({
          series: [
            {
              type: 'gauge',
              radius: '100%',
              center: ['50%', '60%'],
              max: this.tableData.data1.maxPower,
              progress: {
                show: true,
                width: 5
              },
              splitLine: {
                length: 5,
                distance: 0,
                lineStyle: {
                  width: 2,
                  color: '#999'
                }
              },
              axisLine: {
                roundCap: true,
                lineStyle: {
                  color: [[1, '#e7ebf6']]
                }
              },
              axisLabel: {
                fontSize: 10,
                formatter: function(value) {
                  // return parseInt(value)
                  if ((value + '').indexOf('.') != -1) {
                    return value.toFixed(1)
                  } else {
                    return value
                  }
                }
              },
              itemStyle: {
                color: '#1b94ea'
              },
              anchor: {
                show: true,
                showAbove: true,
                size: 15,
                itemStyle: {
                  borderWidth: 5,
                  borderColor: '#1b94ea'
                }
              },
              splitNumber: '4',
              axisTick: {
                show: false
              },
              title: {
                offsetCenter: [0, -20],
                fontSize: 12
              },
              detail: {
                show: false
              },
              data: [
                {
                  value: this.tableData.data1.equipmentPower,
                  name: this.tableData.data1.unit
                }
              ]
            }
          ]
        })
      }
      if (this.tableData.data2.companyDeviceId) {
        if (this.echarts2) {
          this.$echarts.init(this.$refs['EquipmentPower_echartsId2']).dispose()
        }
        this.echarts2 = this.$echarts.init(this.$refs['EquipmentPower_echartsId2'])
        this.echarts2.setOption({
          series: [
            {
              type: 'gauge',
              radius: '100%',
              center: ['50%', '60%'],
              max: this.tableData.data2.maxPower,
              progress: {
                show: true,
                width: 5
              },
              splitLine: {
                length: 5,
                distance: 0,
                lineStyle: {
                  width: 2,
                  color: '#999'
                }
              },
              axisLine: {
                roundCap: true,
                lineStyle: {
                  color: [[1, '#e7ebf6']]
                }
              },
              axisLabel: {
                fontSize: 10,
                formatter: function(value) {
                  if ((value + '').indexOf('.') != -1) {
                    return value.toFixed(1)
                  } else {
                    return value
                  }
                }
              },
              itemStyle: {
                color: '#1b94ea'
              },
              anchor: {
                show: true,
                showAbove: true,
                size: 15,
                itemStyle: {
                  borderWidth: 5,
                  borderColor: '#1b94ea'
                }
              },
              splitNumber: '4',
              axisTick: {
                show: false
              },
              title: {
                offsetCenter: [0, -20],
                fontSize: 12
              },
              detail: {
                show: false
              },
              data: [
                {
                  value: this.tableData.data2.equipmentPower,
                  name: this.tableData.data2.unit
                }
              ]
            }
          ]
        })
      }
      if (this.tableData.data3.companyDeviceId) {
        if (this.echarts3) {
          this.$echarts.init(this.$refs['EquipmentPower_echartsId3']).dispose()
        }
        this.echarts3 = this.$echarts.init(this.$refs['EquipmentPower_echartsId3'])
        this.echarts3.setOption({
          series: [
            {
              type: 'gauge',
              radius: '100%',
              center: ['50%', '60%'],
              max: this.tableData.data3.maxPower,
              progress: {
                show: true,
                width: 5
              },
              splitLine: {
                length: 5,
                distance: 0,
                lineStyle: {
                  width: 2,
                  color: '#999'
                }
              },
              axisLine: {
                roundCap: true,
                lineStyle: {
                  color: [[1, '#e7ebf6']]
                }
              },
              axisLabel: {
                fontSize: 10,
                formatter: function(value) {
                  if ((value + '').indexOf('.') != -1) {
                    return value.toFixed(1)
                  } else {
                    return value
                  }
                }
              },
              itemStyle: {
                color: '#1b94ea'
              },
              anchor: {
                show: true,
                showAbove: true,
                size: 15,
                itemStyle: {
                  borderWidth: 5,
                  borderColor: '#1b94ea'
                }
              },
              splitNumber: '4',
              axisTick: {
                show: false
              },
              title: {
                offsetCenter: [0, -20],
                fontSize: 12
              },
              detail: {
                show: false
              },
              data: [
                {
                  value: this.tableData.data3.equipmentPower,
                  name: this.tableData.data3.unit
                }
              ]
            }
          ]
        })
      }
      if (this.tableData.data4.companyDeviceId) {
        if (this.echarts4) {
          this.$echarts.init(this.$refs['EquipmentPower_echartsId4']).dispose()
        }
        this.echarts4 = this.$echarts.init(this.$refs['EquipmentPower_echartsId4'])
        this.echarts4.setOption({
          series: [
            {
              type: 'gauge',
              radius: '100%',
              center: ['50%', '60%'],
              max: this.tableData.data4.maxPower,
              progress: {
                show: true,
                width: 5
              },
              splitLine: {
                length: 5,
                distance: 0,
                lineStyle: {
                  width: 2,
                  color: '#999'
                }
              },
              axisLine: {
                roundCap: true,
                lineStyle: {
                  color: [[1, '#e7ebf6']]
                }
              },
              axisLabel: {
                fontSize: 10,
                formatter: function(value) {
                  if ((value + '').indexOf('.') != -1) {
                    return value.toFixed(1)
                  } else {
                    return value
                  }
                }
              },
              itemStyle: {
                color: '#1b94ea'
              },
              anchor: {
                show: true,
                showAbove: true,
                size: 15,
                itemStyle: {
                  borderWidth: 5,
                  borderColor: '#1b94ea'
                }
              },
              splitNumber: '4',
              axisTick: {
                show: false
              },
              title: {
                offsetCenter: [0, -20],
                fontSize: 12
              },
              detail: {
                show: false
              },
              data: [
                {
                  value: this.tableData.data4.equipmentPower,
                  name: this.tableData.data4.unit
                }
              ]
            }
          ]
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.largeScreen_main{
  width: 100%;
  height: 100%;
}
  .largeScreen_header{
    display: flex;
    justify-content: space-between;
    .largeScreen_header_title{
      font-size: 20px;
      height: 60px;display: flex;align-items: center;
      font-weight: bold;
      color: #333333;
      padding-left: 20px;
    }
  }
.echarts_noData{
  width: 100%;
  height: calc(100% - 60px);
}
  .echarts_container{
    width: calc(100% - 20px);
    height: calc(100% - 60px);
    display: flex;justify-content: flex-start;align-items: flex-start;
    margin: 0 10px;
  }
  .echarts_item{
    width: calc(25% - 20px);height: calc(100% - 20px);
    background-color: #F4F9FF;
    margin: 0 10px;
    border-radius: 10px;
    box-shadow: inset 0px 0px 10px 0px rgba(43, 98, 203, 0.24);
    .echarts_item_top{
      width: 100%;
      height: 60%;
      display: flex;justify-content: center;align-items: center;
    }
    .echarts_item_content1{
      width: 100%;
      height: 15%;
      display: flex;justify-content: center;align-items: center;
      font-size: 20px;color: #333333;
    }
    .echarts_item_content2{
      width: 100%;
      height: 6%;
      display: flex;justify-content: center;align-items: center;
      font-size: 20px;color: #333333;
    }
    .echarts_item_content3{
      width: 100%;
      height: 15%;
      display: flex;justify-content: center;align-items: center;
      font-size: 18px;color: #666666;
    }
  }
  //.echarts_item:first-child{
  //  margin: 0 10px 20px 20px;
  //}
  //.echarts_item:last-child{
  //  margin: 0 20px 20px 10px;
  //}

</style>

